package sala.commonmovie.rightmenu 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import sala.events.RightMenuEvent;
	import sala.model.VideoInfo;
	
	/**
	 * ...
	 * @author Sun
	 */
	public class RightMenuItem extends MovieClip
	{
		private var videoInfo: VideoInfo;
		private var _active:Boolean = false;
		
		private var loader:Loader;
			
		public function RightMenuItem() 
		{
			this.buttonMode = true;
			this.addEventListener(MouseEvent.CLICK, rightMenuItemClickedHdl);
			loader = new Loader();
			this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImageCompleteHdl);
		}
		
		private function loadImageCompleteHdl(e:Event):void 
		{
			var bitmap:Bitmap = Bitmap(LoaderInfo(e.currentTarget).content);
			bitmap.width = 100;
			bitmap.height = 60;
			while (this.numChildren)
			{
				this.removeChildAt(0);
			}
			
			this.addChild(bitmap);
		}
		
		private function rightMenuItemClickedHdl(e:MouseEvent):void 
		{
			if (!active)
			{
				var evt: RightMenuEvent = new RightMenuEvent(RightMenuEvent.RIGHT_MENU_ITEM_CLICKED);
				evt.videoInfo = videoInfo;
				dispatchEvent(evt);
			}
		}
		
		public function updateRightMenuItem(vInfo:VideoInfo):void
		{
			videoInfo = vInfo;
			
			//while (this.numChildren)
			//{
				//this.removeChildAt(0);
			//}
			
			loader.load(new URLRequest(videoInfo.thumb));
		}
		
		public function get active():Boolean { return _active; }
		
		public function set active(value:Boolean):void 
		{
			_active = value;
			if (value)
			{
				this.alpha = 0.7;
			}
			else
			{
				this.alpha = 1;
			}
		}
	}

}